/**
 * Created by Administrator on 2017/5/11.
 */
import {cityUd} from 'store/modules/types'
import {callCurrentCity, callHotCity, callGroupCity, callSearchPlace} from 'api/client/city_api'

const state = {
  cityDetail:{
    currentCityName: '',
    searchVal: '',
    searchPlaceList: []
  }
}

// getters
const getters = {
  cityDetailState : state => state.cityDetail
}

// actions
const actions = {
  [cityUd.A.INIT_PARAMS]({commit, state}){
    commit(cityUd.INIT_PARAMS)
  },
  [cityUd.A.GET_CURRENT_CITY]({commit, state},number){
    callCurrentCity(number).then( (response) =>{
      if(!_.isEmpty(response)){
        commit(cityUd.GET_CURRENT_CITY,response.name)
      }
    })
  },
  [cityUd.A.GET_SEARCH_VAL]({commit, state},e){
    commit(cityUd.GET_SEARCH_VAL,e)
  },
  [cityUd.A.GET_SEARCH_PLACE]({commit, state},cityId){
    let {searchVal} = state.cityDetail
    callSearchPlace(cityId, searchVal).then( (response) =>{
      commit(cityUd.GET_SEARCH_PLACE,response)
    })
  }
}

// mutations
const mutations = {
  [cityUd.INIT_PARAMS](state){
    state.cityDetail.searchVal = ''
    state.cityDetail.currentCityName = ''
    state.cityDetail.searchPlaceList = []
  },
  [cityUd.GET_CURRENT_CITY](state,currentCityName){
    state.cityDetail.currentCityName = currentCityName
  },
  [cityUd.GET_SEARCH_VAL](state,searchVal){
    state.cityDetail.searchVal = searchVal
  },
  [cityUd.GET_SEARCH_PLACE](state, searchPlaceList){
    state.cityDetail.searchPlaceList = searchPlaceList
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}